use foundation::{id, NSString, ObjCObject};
use objc::{runtime::Object, sel, sel_impl};
use objc_derive::{selector_export, Alloc, Deref, GetObject, Message};

extern "C" {
  fn MTLCreateSystemDefaultDevice() -> *mut MTLDevice;
}

#[repr(transparent)]
#[derive(Clone, Message, GetObject, Deref, Alloc)]
pub struct MTLDevice(pub id);

impl MTLDevice {
  pub fn system_default() -> Option<MTLDevice> {
    unsafe {
      let device = MTLCreateSystemDefaultDevice();
      if device.is_null() {
        None
      } else {
        let ret = device as *const Object as usize as *mut Object;
        Some(MTLDevice(ret))
      }
    }
  }
}

impl MTLDevice {
  #[selector_export("name")]
  pub fn name(&self) -> NSString;
}
